package es

import (
	"fmt"
	"github.com/elastic/go-elasticsearch/v8"
	"os"
)

func GetClient() *elasticsearch.TypedClient {
	cert, err := os.ReadFile("./ca.crt")
	if err != nil {
		dir, _ := os.Getwd()
		fmt.Printf("current dir: %s\n", dir)
		fmt.Printf("Read cert file faild: %s\n", err.Error())
	}
	// ES 配置
	cfg := elasticsearch.Config{
		Addresses: []string{
			"https://10.82.82.52:30920", // 使用https要加证书
		},
		//Username: "elastic",
		//Password: "oP0597M5u6lv249RrT8gDALl",
		CACert: cert,
		APIKey: "MWl2YlpJNEJkYjJZY0hGT3ZUMzc6MjhVU3ZCTUJUNTY5TDBQSEplUXh1Zw==",
		//ServiceToken: "AAEAAWVsYXN0aWMvZW50ZXJwcmlzZS1zZWFyY2gtc2VydmVyL2NvbnRhaW5lci1jbGllbnQ6emVBQ05uekpTZnF6NDROeGUycHRWQQ",
	}

	// 创建客户端连接
	client, err := elasticsearch.NewTypedClient(cfg)
	if err != nil {
		fmt.Printf("elasticsearch.NewTypedClient failed, err:%v\n", err)
		os.Exit(1)
	}
	return client
}
